<template>
    <div class="nav">
        <div class="left" @click="moveBy(navContent, 'l')">
            <el-icon>
                <ArrowLeftBold />
            </el-icon>
        </div>
        <div class="content" ref="nav-content">
            <div class="box">
                <div class="pro-item" v-for="item in categories" :key="item.id"
                    :class="{ active: item.id === category }" @click="handleChange(item.id!)" :title="item.name">
                    <span>{{ item.name }}</span>
                </div>
            </div>
        </div>
        <div class="right" @click="moveBy(navContent, 'r')">
            <el-icon>
                <ArrowRightBold />
            </el-icon>
        </div>
    </div>
</template>
<script lang="ts" setup>
import database from '@/api/database'
import { moveBy, moveCenter } from '@/composables/useHome'
import { ArrowLeftBold, ArrowRightBold } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'

const navContent = useTemplateRef('nav-content')
const categories = ref<Category[]>([])
const category = defineModel<number>('category')
const projects = defineModel<Paginate<Project>>('projects')
const props = defineProps<{
    pageSize: number
}>()
const page = defineModel<number>('page')

async function handleChange(id: number) {
    moveCenter(navContent.value)
    category.value = id;
    (await database()).table('projects').where('category_id', id).paginate<Project>(1, props.pageSize).then(res => {
        projects.value = res
        page.value = 1
    }).catch(err => {
        category.value = 0
        ElMessage.error(err)
    })
}

onMounted(async () => {
    (await database()).table('categories').orderBy("sort", 'asc').all<Category>().then(res => {
        categories.value = res
        if (res.length && !category.value) {
            handleChange(res[0].id!)
        }
    }).catch(err => {
        ElMessage.error(err)
    })
})
</script>
<style lang="scss" scoped>
.nav {
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-wrap: nowrap;
    margin: -20px -20px 0;
    height: 40px;
    border-bottom: 1px solid var(--el-border-color);

    .left,
    .right {
        flex: 0 0 32px;
        font-size: 20px;
        height: 100%;
        line-height: 50px;
        text-align: center;
        user-select: none;
        background-color: var(--el-bg-color);
        z-index: 10;

        &:hover {
            cursor: pointer;
            background: var(--el-menu-hover-bg-color);
        }

        &:active {
            color: var(--el-color-primary);
        }
    }

    .left {
        border-right: 1px solid var(--el-border-color);
    }

    .right {
        border-left: 1px solid var(--el-border-color);
    }

    .content {
        flex: 0 0 calc(100% - 64px);
        height: 100%;
        overflow: hidden;
        z-index: 1;

        .box {
            width: max-content;
            height: 100%;
            display: flex;
            justify-content: flex-start;
            align-items: flex-end;
            transition: all .3s;
            left: 0;
            --bg-color: transparent;
            --bg-active-color: var(--el-color-primary);
            --bg-hover-color: var(--el-color-primary-light-5);

            .pro-item {
                flex-shrink: 0;
                flex-grow: 0;
                height: 100%;
                line-height: 40px;
                padding-left: 15px;
                padding-right: 15px;
                cursor: pointer;
                border-radius: 10px 10px 0 0;
                background: var(--bg-color);
                box-shadow: 0 0 1px 0 var(--el-border-color);

                span {
                    display: block;
                    max-width: 120px;
                    overflow: hidden;
                    white-space: nowrap;
                    text-overflow: ellipsis;
                    background: var(--bg-color);
                    z-index: 1;
                }

                &:not(:first-child)::before {
                    content: '';
                    position: absolute;
                    top: 0;
                    left: -10px;
                    width: 11px;
                    height: 100%;
                    background: var(--bg-color);
                    clip-path: path('M 10 30, Q 10 40, 0 40, L 11 40, Z');
                    z-index: 0;
                }

                &:not(:last-child):after {
                    content: '';
                    position: absolute;
                    top: 0;
                    right: -10px;
                    width: 11px;
                    height: 100%;
                    background: var(--bg-color);
                    clip-path: path('M 1 30, Q 1 40, 11 40, L 0 40,Z');
                    z-index: 0;
                }

                &:hover {
                    --bg-color: var(--bg-hover-color);
                }

                &.active {
                    color: var(--el-color-white);
                    --bg-color: var(--el-color-primary);
                    z-index: 3;
                }
            }
        }
    }
}
</style>